package com.teensoft.core.user.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.teensoft.base.dao.imp.HibernateDAO;
import com.teensoft.base.exception.DAOException;
import com.teensoft.core.user.User;
import com.teensoft.core.user.UserContect;
import com.teensoft.core.user.UserContectDAO;
import com.teensoft.core.user.common.Utils;

public class UserContectDAOImp extends HibernateDAO implements UserContectDAO {

	public void addUserContect(UserContect contect) throws DAOException{
		this.saveOrUpdate(contect);
	}

	public void deleteUserContectById(String id) throws DAOException{
		this.delete(UserContect.class, id);
	}

	public void deleteUserContectByUserId(String user_id) throws DAOException{
		User user = (User)this.get(User.class, user_id);
		Set<UserContect> contect = user.getUser_contact();
		if(!Utils.isNull(contect)){
			UserContect[] cs = contect.toArray(new UserContect[]{});
			String[] cid = new String[contect.size()];
			for(int i = 0;i<contect.size();i++){
				cid[i] = cs[i].getId();
			}
			this.delete(UserContect.class, cid);
		}
	}

	public UserContect getUserContectById(String id) throws DAOException{
		return (UserContect)this.get(UserContect.class, id);
	}

	public List<UserContect> getUserContectByUserId(String user_id) throws DAOException{
		User user = (User)this.get(User.class, user_id);
		Set<UserContect> contect = user.getUser_contact();
		List<UserContect> result = new ArrayList<UserContect>();
		for(UserContect c : contect){
			result.add(c);
		}
		return result;
	}

	public void updateUserContect(UserContect contect) throws DAOException{
		this.saveOrUpdate(contect);
	}

}
